<?php
////////////////////////////////////////////////////////////////////////////////
//	  This file is part of PushLMS.
//
//    PushLMS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    PushLMS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with PushLMS.  If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////

class QuestiongroupsController extends AppController {

	var $scaffold = 'admin';
	var $helpers = array('Form', 'Javascript', 'Ajax', 'Captcha');	
	var $uses = array('Questiongroup', 'Assessment', 'Asession', 'Asessionlog', 'Question', 'Answer', 'AnswerType', 'Formtype', 'User','Course', 'Usergroup');

	function beforeFilter() {

	}

	################
	# POST UPDATE METHODS
	################

	public function reorder(){

		#error_log(print_r($this->data['questiongroups'][0],true));

		$rank = 1;
		$this->log($this->data['questiongroups']);
		foreach ($this->data['questiongroups'][0] as $questiongroup_id)
		{
			error_log("setting rank $rank for $questiongroup_id");
			$this->Questiongroup->id = $questiongroup_id;
			$this->Questiongroup->saveField('rank', $rank);
			$rank ++;
		}

		$this->layout = 'none';
	}

	################
	# AJAX METHODS
	################

	public function ajax_get($id=null){

		if ($this->RequestHandler->isAjax()){

			#error_log('assessmentid:'.$id);

			Configure::write('debug', 0);

			if ($id != null){

				$data = $this->Assessment->findById($id);
				$this->set('assessment', $data);
				$this->set('assessment_id',$id);
				$this->Session->setFlash("");
			}
		}
	}

	public function ajax_edit($id=null){

		error_log('woohoo');

		if ($this->RequestHandler->isAjax()){

			if(!empty($this->data)) {

				#error_log('questionsgroup:'.$id);

				Configure::write('debug', 0);

				if($this->Questiongroup->save($this->data)) {
					$this->Session->setFlash('Question Group Data Updated');
					$this->set('flash', true);
					$id = $this->Questiongroup->id;
					$tmpquestiongroup = $this->Questiongroup->findById($id);
					$tmpassessment = $this->Assessment->findById($tmpquestiongroup['Questiongroup']['assessment_id']);
					//$this->redirect('/questiongroups/ajax_get/'.$tmpassessment['Assessment']['id']);
				}
				else
				{
					$id = $this->data['Questiongroup']['id'];
				}
		
			}

			$questiongroup = $this->Questiongroup->findById($id);
			$assessment = $this->Assessment->findById($questiongroup['Questiongroup']['assessment_id']);

			$this->set('questiongroup',$questiongroup);
			$this->set('assessment',$assessment);
			$this->data = $questiongroup;
			$this->Session->setFlash("Question Group Updated");
		}
	}


	public function ajax_delete($id=null){

		if ($this->RequestHandler->isAjax()){

			Configure::write('debug', 0);

			if ($id != null){

				$questiongroup = $this->Questiongroup->findById($id);
				$this->Questiongroup->delete($id, false);
				$this->ajax_get($questiongroup['Questiongroup']['assessment_id']);

			}
	
		}

	}

	public function ajax_add($assessment_id=null){

		if ($this->RequestHandler->isAjax()){

			Configure::write('debug', 0);

			if ($assessment_id != null){

				#error_log('assesmentid:'.$assessment_id);
	
				$newQuestionGroup = array(
					'Questiongroup' => array(
						'name' => 'new-questions-group',
						'description' => 'new-questions-group',
						'assessment_id' => $assessment_id
					),
				);

				$this->Questiongroup->saveAll($newQuestionGroup);

				#error_log('new question group id:'.$this->Questiongroup->id);

				$questiongroup = $this->Questiongroup->findById($this->Questiongroup->id);
				$this->ajax_get($assessment_id);

			}
	
		}

	}

	public function ajax_duplicate($id=null, $assessment_id=false){

		if ($this->RequestHandler->isAjax()){

			Configure::write('debug', 0);

			if (!empty($id)) {
				
				$questiongroup_old = $this->Questiongroup->findById($id);
				$this->Questiongroup->copy($id);
				$this->Questiongroup->saveField('name',$questiongroup_old['Questiongroup']['name'].'--copy');

				$questiongroup_new = $this->Questiongroup->findById($this->Questiongroup->id);

				foreach ($questiongroup_old['Question'] as $question)
				{
					//$this->Question->copy($question['id']);
					$this->Question->saveField('questiongroup_id', $questiongroup_new['Questiongroup']['id']);
					$question_select = $this->Question->findById($question['id']);

					foreach ($question_select['Answer'] as $answer)
					{
						$this->Answer->copy($answer['id']);
						foreach ($questiongroup_new['Question'] as $question_new)
						{
							$question_select_new = $this->Question->findById($question_new['id']);
							echo ":: " . $question_new['id'] . " ::";
							$this->Answer->saveField('question_id',$question_select_new['Question']['id']);
							#error_log('old qg id: '.$questiongroup_new['Question'][$question_index]['id']);
							#error_log('answer id:'.$question['id'].' answer id new:'.$this->Answer->id);
							unset($this->Answer->id);
						}
					}
					#error_log('old qg id: '.$assessment_new['Questiongroup'][$qgroup_index]['id']);
					#error_log('question id:'.$question['id'].' question id new:'.$this->Question->id);
					unset($this->Question->id);

				}
				/*foreach ($questiongroup_old['Question'] as $question){
					
					$question_select = $this->Question->findById($question['id']);

					foreach ($question_select['Answer'] as $answer){

						$this->Answer->copy($answer['id']);
						$this->Question->saveField('question_id',$questiongroup_new['Question'][$question_index]['id']);
						#error_log('old qg id: '.$questiongroup_new['Question'][$question_index]['id']);
						#error_log('answer id:'.$question['id'].' answer id new:'.$this->Answer->id);
						unset($this->Answer->id);

					}*/

				if ($assessment_id){
					#error_log('assessment_id:'.$assessment_id);
					$this->ajax_get($assessment_id);
				}

			}

		}

	}


}

?>
